package com.voice.client.api;

import com.alibaba.fastjson.JSONObject;
import com.voice.core.api.client.ApacheHttpClient;
import com.voice.core.api.enums.HttpMethod;
import com.voice.core.api.enums.Scheme;
import com.voice.core.api.model.ApiRequest;
import com.voice.core.api.model.ApiResponse;

import java.util.List;
import java.util.Map;


public final class SyncApiClient extends ApacheHttpClient {
    public SyncApiClient(VoiceApiClientBuilderParams builderParams) {
        super.init(builderParams);
    }

    public ApiResponse request(ApiRequest apiRequest,String host,boolean isHttps, Map<String, List<String>> headers,Map<String, String> querys){
        apiRequest.setScheme(isHttps ? Scheme.HTTPS : Scheme.HTTP);
        apiRequest.setHost(host);
        apiRequest.addQuerys(querys);
        apiRequest.addHeader(headers);
        return this.sendSyncRequest(apiRequest);
    }

    public ApiResponse request(HttpMethod method,String host, String path,VoiceApiRequest request,boolean isHttps, Map<String, List<String>> headers,Map<String, String> querys){
        ApiRequest apiRequest = new ApiRequest(method, path);
        if(request!=null){
            byte[] body = JSONObject.toJSONString(request.getParams()).getBytes();
            apiRequest.setBody(body);
            apiRequest.addHeader(request.getHeader());
        }
        return this.request(apiRequest,host,isHttps,headers,null);
    }

    public ApiResponse postFile(String host, String path, VoiceApiRequest<String> request, boolean isHttps, Map<String, List<String>> headers){
        ApiRequest apiRequest = new ApiRequest(HttpMethod.POST_FORM_DATA, path);
        if(request!=null){
            apiRequest.addFormParams(request.getParams());
            apiRequest.addFiles(request.getFiles());
            apiRequest.addHeader(request.getHeader());
        }
        return this.request(apiRequest,host,isHttps,headers,null);
    }


    public ApiResponse postFile(String host, String path, VoiceApiRequest<String> request, boolean isHttps) {
        return this.postFile(host, path, request, isHttps,null);
    }

    public ApiResponse postFile(String host, String path, VoiceApiRequest<String> request) {
        return this.postFile(host, path, request, false, null);
    }

    public ApiResponse postBody(String host, String path, VoiceApiRequest request, boolean isHttps, Map<String, List<String>> headers){
        return this.request(HttpMethod.POST_BODY,host,path,request,isHttps,headers,null);
    }

    public ApiResponse postBody(String host, String path, VoiceApiRequest request, boolean isHttps) {
        return this.postBody(host, path, request, isHttps,null);
    }

    public ApiResponse postBody(String host, String path, VoiceApiRequest request) {
        return this.postBody(host, path, request, false, null);
    }

    public ApiResponse putBody(String host, String path, VoiceApiRequest request, boolean isHttps, Map<String, List<String>> headers){
        return this.request(HttpMethod.PUT_BODY,host,path,request,isHttps,headers,null);
    }

    public ApiResponse putBody(String host, String path, VoiceApiRequest request, boolean isHttps) {
        return this.putBody(host, path, request, isHttps,null);
    }

    public ApiResponse putBody(String host, String path, VoiceApiRequest request) {
        return this.putBody(host, path, request, false, null);
    }

    public ApiResponse getBody(String host, String path, boolean isHttps, Map<String, List<String>> headers, Map<String, String> querys) {
        return request(HttpMethod.GET,host,path,null,isHttps,headers,querys);
    }

    public ApiResponse getBody(String host, String path,boolean isHttps,Map<String, List<String>> headers) {
        return this.getBody(host, path, isHttps,headers,null);
    }

    public ApiResponse getBody(String host, String path,boolean isHttps) {
        return this.getBody(host, path,isHttps,null, null);
    }

    public ApiResponse getBody(String host, String path) {
        return this.getBody(host, path, false, null, null);
    }
}

